// pages/user/user.js
const tool = require("../../utils/tool")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight:'0px',
    contentHeight:'0px',
    loginedUserInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    tool.setThemeColor(this)
    this.setData({
      sysImagURLPrefix:wx.getStorageSync('sysImagURLPrefix'),
      themeSetting:wx.getStorageSync('themeSetting'),
      clearCache:wx.getStorageSync('clearCache'),
      startup:wx.getStorageSync('startup')
    })
    options = JSON.parse(options.data)
    this.setData({
      statusBarHeight:(parseInt(options.statusBarHeight.split('px')[0])+4).toString() + 'px',
      loginedUserInfo:options.loginedUserInfo,
      contentHeight:options.contentHeight
    })
  },

  changeStartUp(){
    wx.showActionSheet({
      itemList: ['主页-最近','主页-巡查','主页-评价','主页-处理','主页-审核','管理'],
      success:(e)=>{
        this.setData({
          startup:['主页-最近','主页-巡查','主页-评价','主页-处理','主页-审核','管理'][e.tapIndex]
        })
        wx.setStorageSync('startup', ['主页-最近','主页-巡查','主页-评价','主页-处理','主页-审核','管理'][e.tapIndex])
      }
    })
  },

  async clearCache(){
    wx.showActionSheet({
      itemList: ['清理超过30天未使用的缓存','清理超过7天未使用的缓存','清理超过3天未使用的缓存','不缓存','不清理','立刻清理'],
      success:res=>{
        res = res.tapIndex
        switch (res) {
          case 0:
            this.setData({
              clearCache:'30'
            })
            wx.setStorageSync('clearCache', '30')
            break;
          case 1:
            this.setData({
              clearCache:'7'
            })
            wx.setStorageSync('clearCache', '7')
            break;
          case 2:
            this.setData({
              clearCache:'3'
            })
            wx.setStorageSync('clearCache', '3')
            break;
          case 3:
            this.setData({
              clearCache:'0'
            })
            wx.setStorageSync('clearCache', '0')
            break;
          case 4:
            this.setData({
              clearCache:'-1'
            })
            wx.setStorageSync('clearCache', '-1')
            break;
          case 5:
            tool.deleteLongTimeNotUseImg()
            tool.showToast('清理成功')
            break;
        }
      }
    })
  },

  changeTheme(){
    wx.showActionSheet({
      itemList: ['浅色','深色','跟随微信'],
      success:res=>{
        switch (res.tapIndex) {
          case 0:
            if(wx.getStorageSync('themeSetting')!='light'){
              wx.showModal({
                title: '提示',
                content: '重新启动小程序以应用新的主题',
                showCancel: false
              })
            }
            wx.setStorageSync('themeSetting', 'light')
            this.setData({
              themeSetting:'light'
            })
            break;
          case 1:
            if(wx.getStorageSync('themeSetting')!='dark'){
              wx.showModal({
                title: '提示',
                content: '重新启动小程序以应用新的主题',
                showCancel: false
              })
            }
            wx.setStorageSync('themeSetting', 'dark')
            this.setData({
              themeSetting:'dark'
            })
            break;
          case 2:
            if(wx.getStorageSync('themeSetting')!='auto'){
              wx.showModal({
                title: '提示',
                content: '重新启动小程序以应用新的主题',
                showCancel: false
              })
            }
            wx.setStorageSync('themeSetting', 'auto')
            this.setData({
              themeSetting:'auto'
            })
            break;
        }
      }
    })
  },

  showChangePassword(){
    wx.navigateTo({
      url: '/pages/changePassword/changePassword?data=' + JSON.stringify({
        statusBarHeight:this.data.statusBarHeight,
        contentHeight:this.data.contentHeight,
        loginedUserInfo:this.data.loginedUserInfo
      }),
    })
  },

  showFeedBack(){
    wx.navigateTo({
      url: '/pages/feedBack/feedBack?data=' + JSON.stringify({
        statusBarHeight:this.data.statusBarHeight,
        contentHeight:this.data.contentHeight,
        loginedUserInfo:this.data.loginedUserInfo
      }),
    })
  },

  reLogin(){
    wx.showModal({
      title: '提示',
      content: '是否退出登录？',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          let re = await tool.requestMethod("/exitLogin",{
            account:this.data.loginedUserInfo.account
          })
          if(re.status==-2){
            wx.showModal({
              title: '提示',
              content: '退出失败',
              showCancel: false
            })
          }else{
            wx.setStorageSync('reLogin', 'true')
            wx.navigateBack()
          }
        }
      }
    })
    
  },

  back(){
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(wx.getStorageSync('reLogin')=='true'){
      wx.navigateBack()
    }
    if(wx.getStorageSync('newName')!=""){
      let t = JSON.parse(JSON.stringify(this.data.loginedUserInfo)) 
      t.name = wx.getStorageSync('newName')
      t.avatarText = t.name.slice(t.name.length-2,t.name.length)
      this.setData({
        loginedUserInfo:t
      })
      wx.removeStorageSync('newName')
    }
  },

  showChangeNamePage(){
    wx.navigateTo({
      url: '/pages/changeName/changeName?data=' + JSON.stringify({
        statusBarHeight:this.data.statusBarHeight,
        contentHeight:this.data.contentHeight,
        loginedUserInfo:this.data.loginedUserInfo
      }),
    })
  },

  showChangePhoneNumber(){
    wx.navigateTo({
      url: '/pages/changePhoneNumber/changePhoneNumber?data=' + JSON.stringify({
        statusBarHeight:this.data.statusBarHeight,
        contentHeight:this.data.contentHeight,
        loginedUserInfo:this.data.loginedUserInfo
      }),
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})